package com.officeFactory;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;

/**
 * Created by wangg on 2017/12/28.
 */
public class WordPrint {


    public static void main (String arg[]) {
        try {
            FileInputStream in = new FileInputStream("F://xingzhengchufa_wztc.doc");// 载入文档
//          FileInputStream in = new FileInputStream("2003.doc");// 载入文档
            POIFSFileSystem pfs = new POIFSFileSystem(in);
            HWPFDocument hwpf = new HWPFDocument(pfs);
            Range range = hwpf.getRange();// 得到文档的读取范围
            TableIterator it = new TableIterator(range);

            FileWriter fileWriter = new FileWriter(new File("result.txt"));


            // 迭代文档中的表格
            while (it.hasNext()) {
                Table tb = (Table) it.next();
                // 迭代行，默认从0开始
                if(tb.numRows()>0) {
                    TableRow tr = tb.getRow(0);
                    // 迭代列，默认从0开始
                    if(tr.numCells()==2) {
                        TableCell td1 = tr.getCell(0);// 取得单元格
                        TableCell td2 = tr.getCell(1);// 取得单元格
                        // 取得单元格的内容
                        String str1 = td1.text().trim();
                        String str2 = td2.text().trim();
                        if(str2!=null&&!"".equals(str2)&&str2.contains("[21][11]")){
                            System.out.println(str1);
                            fileWriter.write(str2+"\n");
                        }
                    } else if(tr.numCells()==3){
                        TableCell td2 = tr.getCell(1);
                        String str2 = td2.text().trim();
                        System.out.println("str2="+str2);
                        fileWriter.write(str2+"\n");
                    }
                } // end for
            } // end while

            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
